package com.jeecg.struts.util;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.jeecg.struts.action.Action;
import com.jeecg.struts.form.ActionForm;


public class ConvertObject {

	
	public static  ActionForm getActionForm(HttpServletRequest req,String formClassName){
		//获取form的class路径
		ActionForm obj =null;
		try {
			Class<?> forName = Class.forName(formClassName);
			obj = (ActionForm)forName.newInstance();
			Field[] declaredFields = forName.getDeclaredFields();
			for(Field field :declaredFields){
				field.setAccessible(true);//字段属�?�都为private,避免安全性检查，故必须进行此操作  
				field.set(obj, req.getParameter(field.getName()));
				field.setAccessible(false);//字段属�?�都为private,避免安全性检查，故必须进行此操作  
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	
	
	
	/**
	 * 根据classname得到实例
	 * @param className
	 * @return
	 */
	public static Action getObjectByClassName(String className){
		Action obj =null;
		try {
			Class<?> forName = Class.forName(className);
		    obj = (Action)forName.newInstance();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
	
	/**
	 * 根据classname获取action实例
	 * @param className
	 * @return
	 */
	public static Action getActionByClassName(String className){
		Action obj =null;
		try {
		    obj = (Action)getObjectByClassName(className);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return obj;
	}
	
	
	public static Object getObject(String className){
		//获取form的class路径
		
		Object obj =null;
		try {
			Class<?> forName = Class.forName(className);
		    obj = forName.newInstance();
			Field[] declaredFields = forName.getDeclaredFields();
			for(Field field :declaredFields){
				System.out.println(field.getName() );
				field.setAccessible(true);//字段属�?�都为private,避免安全性检查，故必须进行此操作  
				field.set(obj, "123");
				field.setAccessible(false);//字段属�?�都为private,避免安全性检查，故必须进行此操作  
 			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return obj;
	}
	
	
	
	public static void main(String[] args) {
		Object obj =getObject("com.jeecg.mvc.actionform.LoginForm");
		System.out.println(obj);
	}
	
}








